<template>
    <div>
      <el-form
        status-icon
        :rules="loginRules"
        ref="loginForm"
        :model="loginForm"
        label-width="100px"
      >
        <el-form-item prop="account" label="用户名：">
          <el-input size="small" v-model="loginForm.account"  auto-complete="off" placeholder="请输入用户名">
            <i slot="prefix" class="icon-yonghu"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码：">
          <el-input
            size="small"
            :type="passwordType"
            v-model="loginForm.password"
            auto-complete="off"
            placeholder="请输入密码"
          >
            <i class="el-icon-view el-input__icon Ishow" slot="suffix"></i>
            <i slot="prefix" class="icon-mima"></i>
          </el-input>
        </el-form-item>
        <ul class="login_tab">
          <li>
            <el-checkbox v-model="checked">记住账号</el-checkbox></li>
          <li>
            <el-button type="primary" size="medium" class="login-submit" @click="handleLogin">登录</el-button>
          </li>
        </ul>
      </el-form>
    </div>
</template>

<script>
  import { GetLogin} from "../../api/login";
  import {setAccessToken,getRole,setRole,removeAccessToken,setuserName} from "../../utils/cacheProcessing"
  import jwtDecode  from 'jwt-decode'
    export default {
        name: "loginUsers",
      data() {
        //调用工具类中验证用户名方法
        const validateUsername = (rule, value, callback) => {
          if (value === "") {
            callback(new Error("请输入用户名"));
          } /*else if (!this.isvalidUsername(value)) {
            callback(new Error("请输入正确的用户名"));
          }*/ else {
            callback();
          }
        };
        return {
          loginForm: {
            account: "",
            password: ""
          },
          checked: false,
          loginRules: {
            account: [
              { required: true, trigger: "blur", validator: validateUsername }
            ],
            password: [
              { required: true, message: "请输入密码", trigger: "blur" },
              { min: 6, message: "密码长度最少为6位", trigger: "blur" }
            ]
          },
          passwordType: "password"
        };
      },
      methods:{
        handleLogin() {

          let requestData = {
            username:this.loginForm.account,
            password:this.loginForm.password
          }
          removeAccessToken();
          GetLogin(requestData).then(res => {
              console.log(res);
              console.log(res.headers.authorization);
            setAccessToken(res.headers.authorization);
            setuserName(jwtDecode(res.headers.authorization).sub)
            setRole(jwtDecode(res.headers.authorization).rol);
            //   setuserName("2018081043");
            //   setRole("ROLE_USER")
              if(getRole()==="ROLE_USER"){
                this.$router.push("/user");
              }
              else if(getRole()==="ROLE_ADMIN"){
                this.$router.push("/administrator");
              }else if(getRole()==="ROLE_SUPERADMIN"){
                this.$router.push("/administrator");
              }
            })
            .catch(err => {
              this.$messageOK("warning", "用户名或密码有误！");
              console.log(err);
            });
        },
      }
    }
</script>

<style lang="scss" scoped>
.login_tab{
  text-align: center;
  li{
    display: inline-block;
    margin: 20px;
  }
}
  .Ishow:hover{
    color: #3498db;
    cursor: pointer;
  }
</style>
